from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter

from meiduo_mall.apps.users.serializers import SKUSerializer
from goods.models import SKU


# GET /categories/?P<category_id>\d+)/skus?pag=xxx&page_size=xxx&ordering=xxx
class SKUListView(ListAPIView):
    serializer_class = SKUSerializer

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id, is_launched=True)

    filter_backends = [OrderingFilter]
    ordering_fields = ('create_time', 'price', 'sales')
